package com.jy.api.service.impl;

import com.jy.api.dao.MemberIntegralTransactionDao;
import com.jy.api.domain.MemberIntegralTransaction;
import com.jy.api.service.MemberIntegralTransactionService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by xiaohua on 2019/5/7.
 */
@Service
public class MemberIntegralTransactionServiceImpl implements MemberIntegralTransactionService {

    @Autowired
    private MemberIntegralTransactionDao transactionDao;

    @Override
    public Page<MemberIntegralTransaction> getList(String storeId, String mobile, String name, String begin, String end, String type, Pageable pageable) {
        Date startDate = new Date();
        Date endDate = new Date();
        if (null != begin && null != end) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
            try {
                startDate = simpleDateFormat.parse(begin);
                endDate = simpleDateFormat.parse(end);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if (StringUtils.equals("ALL", type) && null == begin) {
            return transactionDao.findAllByStoreIdOrderByAddTimeDesc(storeId, pageable);
        } else if (StringUtils.equals("NAME", type) && null == begin) {
            return transactionDao.findAllByMemberNameLikeAndStoreIdOrderByAddTimeDesc(name, storeId, pageable);
        } else if (StringUtils.equals("MOBILE", type) && null == begin) {
            return transactionDao.findAllByStoreIdAndMobileLikeOrderByAddTimeDesc(storeId, mobile, pageable);
        } else if (StringUtils.equals("ALL", type) && null != begin) {
            return transactionDao.findAllByStoreIdAndAddTimeBetweenOrderByAddTimeDesc(storeId, startDate, endDate, pageable);
        } else if (StringUtils.equals("NAME", type) && null != begin) {
            return transactionDao.findAllByStoreIdAndAddTimeBetweenAndMemberNameLikeOrderByAddTimeDesc(storeId, startDate, endDate, name, pageable);
        } else if (StringUtils.equals("MOBILE", type) && null != begin) {
            return transactionDao.findAllByStoreIdAndAddTimeBetweenAndMobileLikeOrderByAddTimeDesc(storeId, startDate, endDate, mobile, pageable);
        }
        return null;
    }
}
